//
// Copyright (C) 1999 Grace Software
//
// The contents of this file are subject to the Grace Software Public
// License Version 1.0 (the "GracePL"); you may not use this file
// except in compliance with the GracePL. You may obtain a copy of the
// GracePL at http://www.homestead.com/javalog/files/license.html
//
// Software distributed under the GracePL is distributed on an "AS IS"
// basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
// the GracePL for the specific language governing rights and
// limitations under the GracePL.
//
// The Original Code is Grace Software's JavaLog code, released March
// 21, 1999.  The Initial Developer of the Original Code is Grace
// Software.  Portions created by Grace Software are Copyright (C)
// 1999 Grace Software. All Rights Reserved.
//
// Contributor(s):
//
package grace.cards;

import java.awt.*;
import java.util.*;

import atraxis.log.Log;


public class GraphicCard extends Component {
    public static int CLUB = 1;
    public static int DIAMOND = 2;
    public static int HEART = 3;
    public static int SPADE = 4;
    public static int JACK = 11;
    public static int QUEEN = 12;
    public static int KING = 13;
    public static int ACE = 1;

    private static Font fontCard10 = new Font("TimesRoman", Font.BOLD, 16);
    private Font fontCardNums = new Font("TimesRoman", Font.BOLD, 23);

    private static int seriesNum;
    private static double sizeOfCard = 1.0;
    private static int width = 85;
    private static int height = 128;
    private static int roundness = 10;
    private static Dimension preferredSize = new Dimension(width, height);

    private Polygon diamond, heart, spade;

    private int number = ACE;
    private int suit = CLUB;
    private Image image;

    public GraphicCard(int number, int suit) {
	this.number = number;
	this.suit = suit;
    }


    private void init() {
	image = createImage(width, height);
	drawCard(image.getGraphics(), number, suit, 0);
    }


    public Dimension getPreferredSize() { return preferredSize; }


    public void paint(Graphics graphics) {
	if (image == null) init();

	graphics.drawImage(image, 0, 0, 45, 75, this);
    }





    //
    // this is a whopper method that does most of the card
    // work.  It is called with the graphic to write on, the
    // value of the card (1-13), the type (suit 1-4), and
    // whether the background is gray or white (0=white, anything
    // else is gray).  A call of 0 for the card will return a
    // blue back.  Since the user makes the graphic in the first
    // place, the user can put anything they want on the back of
    // the card.
    //
    public void drawCard(Graphics osg, int value, int type, int backdrop)
    {
	Image littleImage = createImage(35, 35);
	Graphics littleGraphics = littleImage.getGraphics();

	// clear the images
	if(backdrop==0)
	{
	    osg.setColor(Color.white);
	    littleGraphics.setColor(Color.white);
	}
	else
	{
	    osg.setColor(Color.gray.brighter());
	    littleGraphics.setColor(Color.gray.brighter());
	}
	osg.fillRoundRect(0,0,width,height,20,20);
	littleGraphics.fillRect(0,0,60,60);


	// draw the border of the card
	osg.setColor(Color.black);
	osg.drawRoundRect(0,0,width-1,height-1,20,20);
	//osg.setColor(Color.gray);
	//osg.drawRoundRect(1,1,width-3,height-3,20,20);

	// check for back request first (note that the user
	// can make their own back without requesting this)

	if (value==0)
        {
	    osg.setColor(Color.blue);
	    osg.fillRoundRect(10,10,width-20,height-20,20,20);
	    return;
        }


	if(value==10) osg.setFont(fontCard10); // 10's require smaller fonts
	else osg.setFont(fontCardNums);

	Color color = null;
	switch (type)  // set up the correct color for the suit
	{
	    case 1:
	    case 4:
		color = Color.black;
		break;
	    case 2:
		color = Color.red;
	    case 3:
		break;
	    default:  break;
	}
	osg.setColor(color);
	littleGraphics.setColor(color);


	// draw the little pips
	drawSuit(littleGraphics,14,0,type);
	osg.drawImage(littleImage, 2, 26, 20, 20, this);
	osg.drawImage(littleImage, width-24, height-40, 20, 20, this);

	switch (value)  // draw the value on the card
        {
	    case 11: osg.drawString("J",3,22);
		osg.drawString("J",width-20, height-9);
		break;
	    case 12: osg.drawString("Q",3,22);
		osg.drawString("Q",width-20, height-9);
		break;
	    case 13: osg.drawString("K",3,22);
		osg.drawString("K",width-20, height-9);
		break;
	    case 1:  osg.drawString("A",3,22);
		osg.drawString("A",width-20, height-9);
		break;
	    case 10: osg.drawString(Integer.toString(value),3,22);
		osg.drawString(Integer.toString(value),width-20, height-9);
		break;
	    default: osg.drawString(Integer.toString(value),7,22);
		osg.drawString(Integer.toString(value),width-17, height-9);
		break;
	}

	switch (value)  // draw all the big pips on the card
	{
	    case 1:  // draw the middle pip (it's an Ace)
		drawSuit(osg,37,51,type);
		break;
	    case 2:
		// draw the middle two pips
		drawSuit(osg, 37, 15, type);
		drawSuit(osg, 37, 87, type);
		break;
	    case 3:
		// draw the middle three pips
		drawSuit(osg, 37, 15, type);
		drawSuit(osg, 37,51, type);
		drawSuit(osg, 37,87, type);
		break;

	    case 4:
		// draw the four pips
		drawSuit(osg, 24, 15, type);
		drawSuit(osg, 49,15, type);
		drawSuit(osg, 24, 87, type);
		drawSuit(osg, 49,87, type);
		break;

	    case 5:
		// draw the five pips
		drawSuit(osg, 24, 15, type);
		drawSuit(osg, 49,15, type);
		drawSuit(osg, 24, 87, type);
		drawSuit(osg, 49,87, type);
		drawSuit(osg, 37,51, type);
		break;

	    case 6:
		// draw the two columns of three pips
		drawSuit(osg, 24, 15, type);
		drawSuit(osg, 24,51, type);
		drawSuit(osg, 24,87, type);
		drawSuit(osg, 49, 15, type);
		drawSuit(osg, 49,51, type);
		drawSuit(osg, 49,87, type);
		break;

	    case 7:
		// draw the two columns of three pips and a center pip
		drawSuit(osg, 24, 15, type);
		drawSuit(osg, 24,51, type);
		drawSuit(osg, 24,87, type);
		drawSuit(osg, 49, 15, type);
		drawSuit(osg, 49,51, type);
		drawSuit(osg, 49,87, type);
		drawSuit(osg, 37,33,type);
		break;

	    case 8:
		// draw the left and right columns of pips
		drawSuit(osg, 24, 15, type);
		drawSuit(osg, 49,15, type);
		drawSuit(osg, 24, 87, type);
		drawSuit(osg, 49,87, type);
		drawSuit(osg, 24,39,type);
		drawSuit(osg, 49,39,type);
		drawSuit(osg, 24,63,type);
		drawSuit(osg, 49,63,type);
		break;

	    case 9:
		// draw the left and right columns of pips
		drawSuit(osg, 22, 15, type);
		drawSuit(osg, 51,15, type);
		drawSuit(osg, 22, 87, type);
		drawSuit(osg, 51,87, type);
		drawSuit(osg, 22,39,type);
		drawSuit(osg, 51,39,type);
		drawSuit(osg, 22,63,type);
		drawSuit(osg, 51,63,type);
		// draw the middle pips
		drawSuit(osg, 37, 26, type);
		break;

	    case 10:
		// draw the left and right columns of pips
		drawSuit(osg, 22, 15, type);
		drawSuit(osg, 51,15, type);
		drawSuit(osg, 22, 87, type);
		drawSuit(osg, 51,87, type);
		drawSuit(osg, 22,39,type);
		drawSuit(osg, 51,39,type);
		drawSuit(osg, 22,63,type);
		drawSuit(osg, 51,63,type);
		// draw the middle pips
		drawSuit(osg, 37, 26, type);
		drawSuit(osg, 37, 75, type);
		break;
	    case 11:
		// draw Jack stuff
		osg.setColor(Color.gray);
		drawbody(osg,1);
		osg.drawRect(22,22,width-44,height-44);
		break;
	    case 12:
		// draw Queen stuff
		osg.setColor(Color.gray);
		drawbody(osg,2);
		osg.drawRect(22,22,width-44,height-44);
		break;
	    case 13:
		// draw King stuff
		osg.setColor(Color.gray);
		drawbody(osg,3);
		osg.drawRect(22,22,width-44,height-44);
		break;

	    default: break;
	}
    }

    public void drawSuit(Graphics g, int whereX, int whereY, int type)
    {

	// this method should receive the x and y location of the
	// item that needs drawn.  It should then draw it.  Therefore,
	// it needs to know how to sort out CLUB, hearts, diamonds and spades,
	// and only draw the one requested, at the spot requested.
	// It only draws one pip at a time and therefore must be
	// called repeatedly by the card constructor (in this case
	// the drawCard method)

	setPoints(whereX,whereY);

	// club
	if(type==CLUB)
	{
	    g.setColor(Color.black);
	    g.fillOval(whereX+1,whereY,8,8);
	    g.fillOval(whereX-4,whereY+7,8,8);
	    g.fillOval(whereX+6,whereY+7,8,8);
	    g.fillRect(whereX+3,whereY+5,4,12);
	    g.fillRect(whereX+2,whereY+17,6,2);
	}
	// diamond
	if(type==DIAMOND)
	{
	    g.setColor(Color.red);
	    g.fillPolygon(diamond);
	}
	if(type==HEART)
	{
	    // heart
	    g.setColor(Color.red);
	    g.fillOval(whereX-3,whereY+3,10,10);
	    g.fillOval(whereX+6,whereY+3,10,10);
	    g.fillOval(whereX+3,whereY+7,5,5);
	    g.fillPolygon(heart);
	}
	if(type==SPADE)
	{
	    // spade
	    g.setColor(Color.black);
	    g.fillPolygon(spade);
	    g.fillOval(whereX-3,whereY+6,8,13);
	    g.fillOval(whereX+5,whereY+6,8,13);
	    g.fillRect(whereX+3,whereY+5,4,11);
	    g.fillRect(whereX+4,whereY+15,2,6);
	    g.fillRect(whereX+2,whereY+21,6,2);
	}
    }

    //
    // this method could be simplified, but I left it
    // in case someone wanted to bring in graphics for the
    // body of the face cards.
    //
    public void drawbody(Graphics g, int x)
    {
	Image face = createImage(41, 84);
	Graphics faceGraphics = face.getGraphics();

	Polygon body1=new Polygon();
	body1.addPoint(0,20);
	body1.addPoint(10,16);
	body1.addPoint(28,20);
	body1.addPoint(41,41);
	body1.addPoint(41,63);
	body1.addPoint(31,67);
	body1.addPoint(12,63);
	body1.addPoint(0,42);
	switch (x)
	{
	    case 1:   faceGraphics.setColor(Color.red); break;
	    case 2:   faceGraphics.setColor(Color.green); break;
	    case 3:   faceGraphics.setColor(Color.blue); break;
	    default:  break;
	}
	faceGraphics.fillPolygon(body1);
	faceGraphics.setColor(Color.black);
	faceGraphics.drawPolygon(body1);
	//
	// now that we got the face all painted up, we draw it on
	// the graphics object
	//
	g.drawImage(face, 22, 22, this);

    }


    //
    // to get the pips to look right, Polygons were used.
    //

    public void setPoints(int x, int y)
    {
	diamond=new Polygon();
	heart=new Polygon();
	spade=new Polygon();
	diamond.addPoint(x+6,y+1);
	diamond.addPoint(x-4,y+11);
	diamond.addPoint(x+6,y+21);
	diamond.addPoint(x+16,y+11);
	heart.addPoint(x-3,y+10);
	heart.addPoint(x+7,y+22);
	heart.addPoint(x+16,y+10);
	spade.addPoint(x+5,y);
	spade.addPoint(x-2,y+9);
	spade.addPoint(x+12,y+9);

    }

    public static void main(String[] args) {
	Frame frame = new Frame();
	Panel panel = new Panel();

	frame.add(panel);

	panel.add(new GraphicCard(ACE, HEART));
	panel.add(new GraphicCard(JACK, CLUB));
	panel.add(new GraphicCard(QUEEN, SPADE));
	panel.add(new GraphicCard(KING, DIAMOND));
	panel.add(new GraphicCard(10, SPADE));

	frame.pack();
	frame.show();
    }
}